# Makefile for Short Video Distributed Database Project
# 用于编译、构建和运行分布式数据库项目

.PHONY: help build run clean test install-deps run-coordinator run-node0 run-node1 run-node2 run-django stop-all

# 变量定义
PROJECT_NAME := short-video-distributed-db
COORDINATOR_BIN := ./distributed-database/bin/coordinator
NODE_BIN := ./distributed-database/bin/node
DJANGO_DIR := ./short_video_platform

# 节点配置
NODE0_ID := 0
NODE0_PORT := 50051
NODE1_ID := 1
NODE1_PORT := 50052
NODE2_ID := 2
NODE2_PORT := 50053

COORDINATOR_PORT := 8080
DJANGO_PORT := 8000

# 颜色输出
RED := \033[0;31m
GREEN := \033[0;32m
YELLOW := \033[1;33m
BLUE := \033[0;34m
NC := \033[0m # No Color

# 默认目标
help:
	@echo "$(BLUE)╔════════════════════════════════════════════════════╗$(NC)"
	@echo "$(BLUE)║   短视频分布式数据库项目 - Makefile 命令帮助       ║$(NC)"
	@echo "$(BLUE)╚════════════════════════════════════════════════════╝$(NC)"
	@echo ""
	@echo "$(GREEN)编译相关命令:$(NC)"
	@echo "  make build              - 编译整个项目（coordinator 和 node）"
	@echo "  make build-coordinator  - 仅编译协调器"
	@echo "  make build-node         - 仅编译节点程序"
	@echo "  make install-deps       - 安装 Go 依赖包"
	@echo ""
	@echo "$(GREEN)运行相关命令:$(NC)"
	@echo "  make run                - 启动完整系统（coordinator + 3个节点 + Django）"
	@echo "  make run-coordinator    - 启动协调器（端口 $(COORDINATOR_PORT)）"
	@echo "  make run-node0          - 启动节点 0（RPC 端口 $(NODE0_PORT)）"
	@echo "  make run-node1          - 启动节点 1（RPC 端口 $(NODE1_PORT)）"
	@echo "  make run-node2          - 启动节点 2（RPC 端口 $(NODE2_PORT)）"
	@echo "  make run-django         - 启动 Django 开发服务器（端口 $(DJANGO_PORT)）"
	@echo ""
	@echo "$(GREEN)数据库相关命令:$(NC)"
	@echo "  make init-db            - 初始化数据库"
	@echo "  make clean-db           - 清空数据库"
	@echo ""
	@echo "$(GREEN)清理相关命令:$(NC)"
	@echo "  make clean              - 清理编译产物"
	@echo "  make clean-all          - 清理所有产物（包括数据库）"
	@echo "  make stop-all           - 停止所有运行的进程"
	@echo ""
	@echo "$(GREEN)测试相关命令:$(NC)"
	@echo "  make test               - 运行测试"
	@echo "  make test-query         - 测试查询功能"
	@echo ""
	@echo "$(YELLOW)快速开始:$(NC)"
	@echo "  make setup              - 一键设置（安装依赖 + 编译 + 初始化数据库）"
	@echo "  make full-run           - 一键启动完整系统"
	@echo ""

# ============================================
# 编译相关目标
# ============================================

# 检查必要的目录
check-dirs:
	@mkdir -p ./distributed-database/bin
	@mkdir -p ./short_video_platform

# 编译整个项目
build: check-dirs build-coordinator build-node
	@echo "$(GREEN)✓ 项目编译完成！$(NC)"

# 编译协调器
build-coordinator: check-dirs
	@echo "$(BLUE)正在编译协调器...$(NC)"
	@cd ./distributed-database && go build -o bin/coordinator ./main/coordinator/coordinator_main.go
	@echo "$(GREEN)✓ 协调器编译成功！$(NC)"

# 编译节点程序
build-node: check-dirs
	@echo "$(BLUE)正在编译节点程序...$(NC)"
	@cd ./distributed-database && go build -o bin/node ./main/node/node_main.go
	@echo "$(GREEN)✓ 节点程序编译成功！$(NC)"

# 安装 Go 依赖
install-deps:
	@echo "$(BLUE)正在下载 Go 依赖...$(NC)"
	@cd ./distributed-database && go mod download
	@cd ./distributed-database && go mod tidy
	@echo "$(GREEN)✓ 依赖安装成功！$(NC)"

# ============================================
# 数据库相关目标
# ============================================

# 初始化数据库
init-db:
	@echo "$(BLUE)正在初始化数据库...$(NC)"
	@cd ./distributed-database && sqlite3 db0.sqlite3 < sql/sqlite_init.sql
	@cd ./distributed-database && sqlite3 db1.sqlite3 < sql/sqlite_init.sql
	@cd ./distributed-database && sqlite3 db2.sqlite3 < sql/sqlite_init.sql
	@echo "$(GREEN)✓ 数据库初始化完成！$(NC)"
	@echo "  - db0.sqlite3 已创建"
	@echo "  - db1.sqlite3 已创建"
	@echo "  - db2.sqlite3 已创建"

# 清空数据库
clean-db:
	@echo "$(YELLOW)正在清空数据库...$(NC)"
	@rm -f ./distributed-database/db0.sqlite3
	@rm -f ./distributed-database/db1.sqlite3
	@rm -f ./distributed-database/db2.sqlite3
	@echo "$(GREEN)✓ 数据库已清空！$(NC)"

# ============================================
# 运行相关目标
# ============================================

# 启动协调器
run-coordinator: build-coordinator
	@echo "$(BLUE)启动协调器（监听端口 $(COORDINATOR_PORT)）...$(NC)"
	@cd ./distributed-database && $(COORDINATOR_BIN)

# 启动节点 0
run-node0: build-node
	@echo "$(BLUE)启动节点 0（RPC 端口 $(NODE0_PORT)）...$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE0_ID) $(NODE0_PORT)

# 启动节点 1
run-node1: build-node
	@echo "$(BLUE)启动节点 1（RPC 端口 $(NODE1_PORT)）...$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE1_ID) $(NODE1_PORT)

# 启动节点 2
run-node2: build-node
	@echo "$(BLUE)启动节点 2（RPC 端口 $(NODE2_PORT)）...$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE2_ID) $(NODE2_PORT)

# 启动 Django 服务器
run-django:
	@echo "$(BLUE)启动 Django 开发服务器（监听端口 $(DJANGO_PORT)）...$(NC)"
	@cd $(DJANGO_DIR) && python manage.py runserver 0.0.0.0:$(DJANGO_PORT)

# 完整运行（需要在不同的终端中运行各个命令）
run: build init-db
	@echo "$(GREEN)╔════════════════════════════════════════════════════╗$(NC)"
	@echo "$(GREEN)║         完整系统启动指南                           ║$(NC)"
	@echo "$(GREEN)╚════════════════════════════════════════════════════╝$(NC)"
	@echo ""
	@echo "$(YELLOW)请在不同的终端中运行以下命令来启动完整系统:$(NC)"
	@echo ""
	@echo "$(BLUE)终端 1 - 启动协调器:$(NC)"
	@echo "  make run-coordinator"
	@echo ""
	@echo "$(BLUE)终端 2 - 启动节点 0:$(NC)"
	@echo "  make run-node0"
	@echo ""
	@echo "$(BLUE)终端 3 - 启动节点 1:$(NC)"
	@echo "  make run-node1"
	@echo ""
	@echo "$(BLUE)终端 4 - 启动节点 2:$(NC)"
	@echo "  make run-node2"
	@echo ""
	@echo "$(BLUE)终端 5 - 启动 Django:$(NC)"
	@echo "  make run-django"
	@echo ""
	@echo "$(GREEN)所有服务启动后，访问 http://localhost:$(DJANGO_PORT) 即可使用系统$(NC)"
	@echo ""

# ============================================
# 一键启动（仅适用于支持后台运行的系统）
# ============================================

full-run: build init-db
	@echo "$(GREEN)一键启动完整系统...$(NC)"
	@echo "$(YELLOW)注意：此命令仅在支持后台进程的系统上有效$(NC)"
	@echo ""
	@cd ./distributed-database && $(COORDINATOR_BIN) &
	@sleep 2
	@echo "$(GREEN)✓ 协调器已启动$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE0_ID) $(NODE0_PORT) &
	@sleep 1
	@echo "$(GREEN)✓ 节点 0 已启动$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE1_ID) $(NODE1_PORT) &
	@sleep 1
	@echo "$(GREEN)✓ 节点 1 已启动$(NC)"
	@cd ./distributed-database && $(NODE_BIN) $(NODE2_ID) $(NODE2_PORT) &
	@sleep 1
	@echo "$(GREEN)✓ 节点 2 已启动$(NC)"
	@cd $(DJANGO_DIR) && python manage.py runserver 0.0.0.0:$(DJANGO_PORT) &
	@sleep 2
	@echo "$(GREEN)✓ Django 已启动$(NC)"
	@echo ""
	@echo "$(GREEN)╔════════════════════════════════════════════════════╗$(NC)"
	@echo "$(GREEN)║         完整系统已启动！                           ║$(NC)"
	@echo "$(GREEN)╚════════════════════════════════════════════════════╝$(NC)"
	@echo ""
	@echo "$(BLUE)访问地址:$(NC)"
	@echo "  - Web 界面: http://localhost:$(DJANGO_PORT)"
	@echo "  - API 端点: http://localhost:$(COORDINATOR_PORT)/api/request"
	@echo ""
	@echo "$(YELLOW)要停止所有进程，请运行: make stop-all$(NC)"
	@echo ""

# ============================================
# 清理相关目标
# ============================================

# 清理编译产物
clean:
	@echo "$(YELLOW)正在清理编译产物...$(NC)"
	@rm -rf ./distributed-database/bin
	@echo "$(GREEN)✓ 清理完成！$(NC)"

# 清理所有产物
clean-all: clean clean-db
	@echo "$(YELLOW)正在清理所有产物...$(NC)"
	@echo "$(GREEN)✓ 所有产物已清理！$(NC)"

# 停止所有运行的进程
stop-all:
	@echo "$(YELLOW)停止所有运行的进程...$(NC)"
	@pkill -f "$(COORDINATOR_BIN)" || true
	@pkill -f "$(NODE_BIN)" || true
	@pkill -f "manage.py runserver" || true
	@echo "$(GREEN)✓ 所有进程已停止！$(NC)"

# ============================================
# 测试相关目标
# ============================================

# 运行测试
test: build
	@echo "$(BLUE)运行测试...$(NC)"
	@cd ./distributed-database && go test ./...
	@echo "$(GREEN)✓ 测试完成！$(NC)"

# 测试查询功能
test-query: run-coordinator run-node0
	@echo "$(BLUE)测试查询功能...$(NC)"
	@echo "发送测试查询请求..."
	@curl -X POST http://localhost:8080/api/request \
		-H "Content-Type: application/json" \
		-d '{"request_id":"test-1","operation":"query","database":"short_video","table":"user","condition":{"username":"alice"}}'
	@echo ""
	@echo "$(GREEN)✓ 查询测试完成！$(NC)"

# ============================================
# 一键设置
# ============================================

setup: install-deps build init-db
	@echo ""
	@echo "$(GREEN)╔════════════════════════════════════════════════════╗$(NC)"
	@echo "$(GREEN)║         项目设置完成！                             ║$(NC)"
	@echo "$(GREEN)╚════════════════════════════════════════════════════╝$(NC)"
	@echo ""
	@echo "$(YELLOW)下一步:$(NC)"
	@echo "  运行 'make run' 查看如何启动完整系统"
	@echo "  或运行 'make full-run' 一键启动系统"
	@echo ""

# ============================================
# 其他有用的目标
# ============================================

# 查看项目状态
status:
	@echo "$(BLUE)项目状态:$(NC)"
	@echo ""
	@echo "$(BLUE)编译产物:$(NC)"
	@ls -lh ./distributed-database/bin/ 2>/dev/null || echo "  - 尚未编译"
	@echo ""
	@echo "$(BLUE)数据库文件:$(NC)"
	@ls -lh ./distributed-database/db*.sqlite3 2>/dev/null || echo "  - 尚未初始化"
	@echo ""
	@echo "$(BLUE)运行中的进程:$(NC)"
	@pgrep -f "coordinator\|$(NODE_BIN)\|manage.py" || echo "  - 无运行中的进程"
	@echo ""

# 查看日志
logs:
	@echo "$(BLUE)查看运行日志...$(NC)"
	@tail -f ./logs/*.log 2>/dev/null || echo "暂无日志文件"

# 版本信息
version:
	@echo "$(BLUE)项目版本信息:$(NC)"
	@cd ./distributed-database && go version
	@python --version
	@echo ""

# ============================================
# .PHONY 声明
# ============================================

.PHONY: help build build-coordinator build-node install-deps check-dirs
.PHONY: init-db clean-db
.PHONY: run run-coordinator run-node0 run-node1 run-node2 run-django full-run
.PHONY: clean clean-all stop-all
.PHONY: test test-query
.PHONY: setup status logs version

